package com.hisense.hiioc.hismartplate.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hisense.hiioc.hismartplate.entity.dto.AppRetentionInfo;
import com.hisense.hiioc.hismartplate.robot.factory.CacheFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @Auther:biexiande@hisense.com
 * @date:2019/3/18 9:59
 * @des
 */

@Service
public class DeviceLogService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final String LINE_SPLITER ="\n";
    private static final Logger logger= LoggerFactory.getLogger(DeviceLogService.class);

    public void processLog(String logContent){
        String deviceId ="";
        String [] logItems =logContent.split(LINE_SPLITER);
        ObjectMapper mapper = new ObjectMapper();
        for (String logItem :logItems){
            Map<String,String> logInfoMap = null;
            try {
                logInfoMap = mapper.readValue(logItem, HashMap.class);
            } catch (IOException e) {
                logger.error("日志文件序列化异常");
            }
            String logType = logInfoMap.get("type");

            if ("basic".equals(logType)){
                 deviceId = logInfoMap.get("device");
                 continue;
            }
            AppRetentionInfo appRetentionInfo = new AppRetentionInfo();
            if("service".equals(logType)){
                String subType = logInfoMap.get("sub_type");
                String pageName = logInfoMap.get("page_name");
                appRetentionInfo.setDevice(deviceId);
                appRetentionInfo.setServiceName(pageName);
                String cacheKey =deviceId+":"+pageName;
                if("resume".equals(subType)){
                    Long accessTime =Long.parseLong(logInfoMap.get("time"));
                    appRetentionInfo.setUrl(logInfoMap.get("url"));
                    appRetentionInfo.setTimestamp(accessTime);
                    rabbitTemplate.convertAndSend("app_access_info",appRetentionInfo);
                    CacheFactory.INSTANCE.clickEventCache().put(cacheKey,accessTime);
                }
                if("pause".equals(subType)){
                    Long endTime =Long.parseLong(logInfoMap.get("time"));
                    Long startTime =null;
                    try {
                        startTime =(Long)(CacheFactory.INSTANCE.clickEventCache().get(cacheKey));
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }finally {
                        CacheFactory.INSTANCE.clickEventCache().invalidate(cacheKey);
                    }
                    if(null!=startTime){
                        long retention =endTime-startTime;
                        appRetentionInfo.setRetention(retention);
                        rabbitTemplate.convertAndSend("app_access_info",appRetentionInfo);
                    }

                }
            }else if ("button".equals(logType)){

            }
        }

    }
}